<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Services\CustomsClearance;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Gate;
use App\Interfaces\PushTarrifService;
use App\Interfaces\OrderService;
use App\Models\Order;
use App\Models\PushTarrif;

/**
 * 推关订单
 *
 * @date 2016/03/17
 * @author Leen
 */
class PushTarrifController extends Controller
{
    public function push_tarrif_query(){

        if (!Gate::allows('push_tarrif','')) {
            throw new PermissionDeniedException();
        }//end if()
    }//end func order_query()

    /**
     * 推关订单列表
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request,CustomsClearance $customsClearance)
    {

        $this->push_tarrif_query();
        $filter = [
            'order_sn' => $request->get('order_sn'),
            'push_tarrif_status'=>$request->get('push_tarrif_status'),
        ];


//        $query = PushTarrif::orderBy('add_time', 'desc');
        $query = PushTarrif::select('push_tarrif.*', 'order.shipping_num')
                            ->leftJoin('order','push_tarrif.order_sn','=','order.order_sn')
                            ->orderBy('add_time', 'desc');
        //订单单号
        if ($filter['order_sn']){
            $query = $query->where('push_tarrif.order_sn',$filter['order_sn']);
        }

        //推关状态
        if ($filter['push_tarrif_status']){
            $query = $query->where('push_tarrif.push_tarrif_status',$filter['push_tarrif_status']);
        }
        $orderInfo = $query->paginate(10);


        return view('admin.order.pushTarrif', compact(['orderInfo', 'filter']));
    }//end func

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $order_id, PushTarrifService $pushTarrifService)
    {
        $this->push_tarrif_query();

        $push_tarrif_status = 3;
        $pushTarrifService->updatePushTarrif($order_id, $push_tarrif_status, '');

        return response()->json(['status'=>'success','message'=>'推送取消成功']);

    }//end func update

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

//    public function getStatus(Request $request,OrderService $orderService){
//
//        $order_id=$request->get('order_id');
//        $order = Order::find($order_id);
//
//        $order_sn = $order->order_sn;
//        $shipping_num = $order->shipping_num;
//        $request_time = date('Y-m-d H:i:s',time());
//
//        $customsClearanceStatus = $orderService->getCustomsClearanceStatus($order_sn,$shipping_num,$request_time);
//        $customsClearanceStatusJson = json_encode($customsClearanceStatus);
//
//        return $customsClearanceStatusJson;
//
//    }
}
